package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.List;

public class UserGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;
    private int nums;

    public UserGridViewAdapter(Context mContext, List<String> mItems, int nums) {
        super();
        this.mContext = mContext;
        this.mList = mItems;
        this.nums = nums;
    }


    @Override
    public int getCount() {
        if (mList == null) {
            return 0;
        } else {
            return this.mList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mList == null) {
            return null;
        } else {
            return this.mList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from
                    (this.mContext).inflate(R.layout.gridview_item, null, false);
            holder.itemView = (LinearLayout) convertView.findViewById(R.id.gridview_item_view);
            holder.iv = (ImageView) convertView.findViewById(R.id.gridview_item_iv);
            holder.num = (TextView) convertView.findViewById(R.id.gridview_item_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final String itemUrl = mList.get(position);
        holder.num.setText(nums + "");
        holder.iv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, UIHelper.getHigh(mContext)));
        holder.itemView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, UIHelper.getHigh(mContext)));
        if (position == 2) {
            holder.itemView.setVisibility(View.VISIBLE);
        } else {
            holder.itemView.setVisibility(View.GONE);
        }
        String paths = YNUrlHelper.getStatusUrl(mContext, itemUrl);
        String str1 = StringUtils.repaceImgUrl(paths);
//        ImageOptHelper.showFrescoStatus(Uri.parse(str1), holder.iv, mContext);
        ImageOptHelper.showFrescoStatus(str1, holder.iv, mContext);
        return convertView;
    }

    private class ViewHolder {
        LinearLayout itemView;
        ImageView iv;
        TextView num;
    }


}

